package com.dianrui.firemenmonitor.activity;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dianrui.firemenmonitor.Constant;
import com.dianrui.firemenmonitor.R;
import com.dianrui.firemenmonitor.adapter.LateAdapter;
import com.dianrui.firemenmonitor.adapter.LateEarlyAdapter;
import com.dianrui.firemenmonitor.adapter.MissCardAdapter;
import com.dianrui.firemenmonitor.adapter.PunchAttendanceAdapter;
import com.dianrui.firemenmonitor.adapter.PunchStatisicsAdapter;
import com.dianrui.firemenmonitor.bean.AttedanceContent;
import com.dianrui.firemenmonitor.bean.Late;
import com.dianrui.firemenmonitor.bean.LeaveEarly;
import com.dianrui.firemenmonitor.bean.MissCard;
import com.dianrui.firemenmonitor.util.OkHttpUtils;
import com.dianrui.firemenmonitor.util.StringUtils;
import com.dianrui.firemenmonitor.util.Url;
import com.dianrui.firemenmonitor.view.InnerListview;
import com.dianrui.firemenmonitor.view.TrackDatePicker;
import com.google.gson.JsonObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 打卡统计更多
 */
public class PunchStatisticsActivity extends BaseActivity {
    private List<Late> LateLists = new ArrayList<>();
    private List<LeaveEarly> leaveearlyLists = new ArrayList<>();
    private List<MissCard> misscardLists = new ArrayList<>();
    private List<AttedanceContent> attedanceContentArrayList = new ArrayList<>();

    private List<Late> DatelateLists = new ArrayList<>();
    private List<LeaveEarly> DateleaveearlyLists = new ArrayList<>();
    private List<MissCard> DatelmisscardLists = new ArrayList<>();
    private List<AttedanceContent> DatelattedanceContentArrayList = new ArrayList<>();


    private LateAdapter lateAdapter;
    private LateEarlyAdapter lateEarlyAdapter;
    private MissCardAdapter missCardAdapter;
    private PunchStatisicsAdapter punchStatisicsAdapter;
    private PunchAttendanceAdapter punchAttendaceAdapter;
    private String times;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.clockindays)
    TextView clockindays;
    @BindView(R.id.lateearlyleave)
    TextView lateearlyleave;
    @BindView(R.id.absence)
    TextView absence;
    @BindView(R.id.attendance_num)
    TextView attendanceNum;
    @BindView(R.id.current_date)
    TextView currentDate;
    @BindView(R.id.late_list)
    InnerListview lateList;
    @BindView(R.id.late_layout)
    LinearLayout lateLayout;
    @BindView(R.id.leave_list)
    InnerListview leaveList;
    @BindView(R.id.leave_layout)
    LinearLayout leaveLayout;
    @BindView(R.id.misscard_list)
    InnerListview misscardList;
    @BindView(R.id.misscard_layout)
    LinearLayout misscardLayout;
    @BindView(R.id.attendance_list)
    InnerListview attendanceList;
    @BindView(R.id.attendance_layout)
    LinearLayout attendanceLayout;
    @BindView(R.id.no_attendce_layout)
    LinearLayout noAttendce_layout;


    @Override
    public int getContentViewId() {
        return R.layout.activity_punchstatistics;
    }

    @Override
    public void initView() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        Date date = new Date(System.currentTimeMillis());
        currentDate.setText(simpleDateFormat.format(date));
        customInit(R.color.blue_518bfa);
        title.setText(getString(R.string.punchstatics_title));

        lateAdapter = new LateAdapter(PunchStatisticsActivity.this, LateLists);
        lateList.setAdapter(lateAdapter);

        lateEarlyAdapter = new LateEarlyAdapter(PunchStatisticsActivity.this, leaveearlyLists);
        leaveList.setAdapter(lateEarlyAdapter);

        missCardAdapter = new MissCardAdapter(PunchStatisticsActivity.this, misscardLists);
        misscardList.setAdapter(missCardAdapter);

        punchAttendaceAdapter = new PunchAttendanceAdapter(PunchStatisticsActivity.this, attedanceContentArrayList);
        attendanceList.setAdapter(punchAttendaceAdapter);

        getDaliyAttendceList();
    }


    @SuppressLint("SimpleDateFormat")
    private void getDaliyAttendceList() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        Date date = new Date(System.currentTimeMillis());
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("times", simpleDateFormat.format(date));
        OkHttpUtils.getInstance().postWithMemberToken(Url.DALIYATTENDACELIST, jsonObject.toString(), new OkHttpUtils.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject && !isFinishing()) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("code"))) {
                                clockindays.setText(jsonObject.optJSONObject("data").optJSONObject("nums").optString("clockindays_nums"));
                                lateearlyleave.setText(jsonObject.optJSONObject("data").optJSONObject("nums").optString("late_nums"));
                                absence.setText(jsonObject.optJSONObject("data").optJSONObject("nums").optString("absenceduty_nums"));
                                attendanceNum.setText(jsonObject.optJSONObject("data").optJSONObject("nums").optString("attendance_nums"));


                                JSONArray late_content = jsonObject.optJSONObject("data").optJSONObject("content").optJSONArray("late_content");
                                if (late_content.length() > 0) {
                                    if (LateLists != null) {
                                        LateLists.clear();
                                    }
                                    for (int i = 0; i < late_content.length(); i++) {
                                        Late punchStatisics = new Late();
                                        JSONObject jsonObject1 = late_content.optJSONObject(i);
                                        punchStatisics.onehitcard = jsonObject1.optString("onehitcard");
                                        LateLists.add(punchStatisics);
                                    }
                                    if (lateAdapter != null) {
                                        lateAdapter.refresh(LateLists);
                                    }
                                    lateLayout.setVisibility(View.VISIBLE);
                                } else {
                                    lateLayout.setVisibility(View.GONE);
                                }

                                JSONArray lates_contents = jsonObject.optJSONObject("data").optJSONObject("content").optJSONArray("lates_contents");
                                if (lates_contents.length() > 0) {
                                    if (leaveearlyLists != null) {
                                        leaveearlyLists.clear();
                                    }
                                    for (int i = 0; i < lates_contents.length(); i++) {
                                        JSONObject jsonObject1 = lates_contents.optJSONObject(i);
                                        LeaveEarly leaveEarly = new LeaveEarly();
                                        leaveEarly.onehitcard = jsonObject1.optString("onehitcard");
                                        leaveearlyLists.add(leaveEarly);
                                    }
                                    if (lateEarlyAdapter != null) {
                                        lateEarlyAdapter.refresh(leaveearlyLists);
                                    }
                                    leaveLayout.setVisibility(View.VISIBLE);
                                } else {
                                    leaveLayout.setVisibility(View.GONE);
                                }


                                JSONArray absenceduty_content = jsonObject.optJSONObject("data").optJSONObject("content").optJSONArray("absenceduty_content");
                                if (absenceduty_content.length() > 0) {
                                    if (misscardLists != null) {
                                        misscardLists.clear();
                                    }
                                    for (int i = 0; i < absenceduty_content.length(); i++) {
                                        JSONObject jsonObject1 = absenceduty_content.optJSONObject(i);
                                        MissCard missCard = new MissCard();
                                        missCard.onehitcard = jsonObject1.optString("onehitcard");
                                        misscardLists.add(missCard);
                                    }
                                    if (missCardAdapter != null) {
                                        missCardAdapter.refresh(misscardLists);
                                    }
                                    misscardLayout.setVisibility(View.VISIBLE);
                                } else {
                                    misscardLayout.setVisibility(View.GONE);
                                }


                                JSONArray attendance_content = jsonObject.optJSONObject("data").optJSONObject("content").optJSONArray("attendance_content");
                                if (attedanceContentArrayList != null) {
                                    attedanceContentArrayList.clear();
                                }
                                if (attendance_content.length() > 0) {
                                    for (int a = 0; a < attendance_content.length(); a++) {
                                        JSONArray jsonObject1 = attendance_content.optJSONArray(a);
                                        if (jsonObject1.length() > 0) {
                                            for (int i = 0; i < jsonObject1.length(); i++) {
                                                AttedanceContent attedanceContent = new AttedanceContent();
                                                JSONArray jsonArray = jsonObject1.optJSONArray(i);
                                                if (jsonArray.length() > 0) {
                                                    for (int j = 0; j < jsonArray.length(); j++) {
                                                        JSONObject jsonObject2 = jsonArray.optJSONObject(j);
                                                        attedanceContent.id = jsonObject2.optString("Id");
                                                        attedanceContent.todaysdate = jsonObject2.optString("inputtime");
                                                        attedanceContent.address = jsonObject2.optString("address");
                                                        attedanceContentArrayList.add(attedanceContent);

                                                    }
                                                }

                                            }


                                        }
                                    }
                                    if (punchAttendaceAdapter != null) {
                                        punchAttendaceAdapter.refresh(attedanceContentArrayList);
                                    }
                                    attendanceLayout.setVisibility(View.VISIBLE);
                                } else {
                                    attendanceLayout.setVisibility(View.GONE);
                                }
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    private void getDateAttendceList(String times) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("times", times);
        OkHttpUtils.getInstance().postWithMemberToken(Url.DALIYATTENDACELIST, jsonObject.toString(), new OkHttpUtils.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject && !isFinishing()) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("code"))) {
                                clockindays.setText(jsonObject.optJSONObject("data").optJSONObject("nums").optString("clockindays_nums"));
                                lateearlyleave.setText(jsonObject.optJSONObject("data").optJSONObject("nums").optString("late_nums"));
                                absence.setText(jsonObject.optJSONObject("data").optJSONObject("nums").optString("absenceduty_nums"));
                                attendanceNum.setText(jsonObject.optJSONObject("data").optJSONObject("nums").optString("attendance_nums"));

                                JSONArray late_content = jsonObject.optJSONObject("data").optJSONObject("content").optJSONArray("late_content");
                                if (late_content.length() > 0) {
                                    if (DatelateLists != null) {
                                        DatelateLists.clear();
                                    }
                                    for (int i = 0; i < late_content.length(); i++) {
                                        Late punchStatisics = new Late();
                                        JSONObject jsonObject1 = late_content.optJSONObject(i);
                                        punchStatisics.onehitcard = jsonObject1.optString("onehitcard");
                                        DatelateLists.add(punchStatisics);
                                    }
                                    if (lateAdapter != null) {
                                        lateAdapter.refresh(DatelateLists);
                                    }
                                    lateLayout.setVisibility(View.VISIBLE);
                                } else {
                                    lateLayout.setVisibility(View.GONE);
                                }

                                JSONArray lates_contents = jsonObject.optJSONObject("data").optJSONObject("content").optJSONArray("lates_contents");
                                if (lates_contents.length() > 0) {
                                    if (DateleaveearlyLists != null) {
                                        DateleaveearlyLists.clear();
                                    }
                                    for (int i = 0; i < lates_contents.length(); i++) {
                                        JSONObject jsonObject1 = lates_contents.optJSONObject(i);
                                        LeaveEarly leaveEarly = new LeaveEarly();
                                        leaveEarly.onehitcard = jsonObject1.optString("onehitcard");
                                        DateleaveearlyLists.add(leaveEarly);
                                    }
                                    if (lateEarlyAdapter != null) {
                                        lateEarlyAdapter.refresh(DateleaveearlyLists);
                                    }
                                    leaveLayout.setVisibility(View.VISIBLE);
                                } else {
                                    leaveLayout.setVisibility(View.GONE);
                                }


                                JSONArray absenceduty_content = jsonObject.optJSONObject("data").optJSONObject("content").optJSONArray("absenceduty_content");
                                if (absenceduty_content.length() > 0) {
                                    if (DatelmisscardLists != null) {
                                        DatelmisscardLists.clear();
                                    }
                                    for (int i = 0; i < absenceduty_content.length(); i++) {
                                        JSONObject jsonObject1 = absenceduty_content.optJSONObject(i);
                                        MissCard missCard = new MissCard();
                                        missCard.onehitcard = jsonObject1.optString("onehitcard");
                                        DatelmisscardLists.add(missCard);
                                    }
                                    if (missCardAdapter != null) {
                                        missCardAdapter.refresh(DatelmisscardLists);
                                    }
                                    misscardLayout.setVisibility(View.VISIBLE);
                                } else {
                                    misscardLayout.setVisibility(View.GONE);
                                }


                                JSONArray attendance_content = jsonObject.optJSONObject("data").optJSONObject("content").optJSONArray("attendance_content");
                                if (DatelattedanceContentArrayList != null) {
                                    DatelattedanceContentArrayList.clear();
                                }
                                if (attendance_content.length() > 0) {
                                    for (int a = 0; a < attendance_content.length(); a++) {
                                        JSONArray jsonObject1 = attendance_content.optJSONArray(a);
                                        if (jsonObject1.length() > 0) {
                                            for (int i = 0; i < jsonObject1.length(); i++) {
                                                AttedanceContent attedanceContent = new AttedanceContent();
                                                JSONArray jsonArray = jsonObject1.optJSONArray(i);
                                                if (jsonArray.length() > 0) {
                                                    for (int j = 0; j < jsonArray.length(); j++) {
                                                        JSONObject jsonObject2 = jsonArray.optJSONObject(j);
                                                        attedanceContent.id = jsonObject2.optString("Id");
                                                        attedanceContent.todaysdate = jsonObject2.optString("inputtime");
                                                        attedanceContent.address = jsonObject2.optString("address");
                                                        DatelattedanceContentArrayList.add(attedanceContent);

                                                    }
                                                }

                                            }


                                        }
                                    }
                                    if (punchAttendaceAdapter != null) {
                                        punchAttendaceAdapter.refresh(DatelattedanceContentArrayList);
                                    }
                                    attendanceLayout.setVisibility(View.VISIBLE);
                                } else {
                                    attendanceLayout.setVisibility(View.GONE);
                                }
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }


    @OnClick({R.id.back, R.id.calendar_img})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.calendar_img:
                TrackDatePicker datePicker = new TrackDatePicker(PunchStatisticsActivity.this);
                datePicker.setDateListener(new TrackDatePicker.OnDateCListener() {

                    @Override
                    public void onDateSelected(String year, String month, String day) {
                        String times = year + "-" + month;
                        currentDate.setText(year + "-" + month);

                        lateAdapter = new LateAdapter(PunchStatisticsActivity.this, DatelateLists);
                        lateList.setAdapter(lateAdapter);

                        lateEarlyAdapter = new LateEarlyAdapter(PunchStatisticsActivity.this, DateleaveearlyLists);
                        leaveList.setAdapter(lateEarlyAdapter);

                        missCardAdapter = new MissCardAdapter(PunchStatisticsActivity.this, DatelmisscardLists);
                        misscardList.setAdapter(missCardAdapter);

                        punchAttendaceAdapter = new PunchAttendanceAdapter(PunchStatisticsActivity.this, DatelattedanceContentArrayList);
                        attendanceList.setAdapter(punchAttendaceAdapter);
                        getDateAttendceList(times);


                    }
                });
                datePicker.show();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (LateLists != null) {
            LateLists.clear();
        }
        if (leaveearlyLists != null) {
            leaveearlyLists.clear();
        }
        if (misscardLists != null) {
            misscardLists.clear();
        }
        if (attedanceContentArrayList != null) {
            attedanceContentArrayList.clear();
        }
        if (DatelateLists != null) {
            DatelateLists.clear();
        }
        if (DateleaveearlyLists != null) {
            DateleaveearlyLists.clear();
        }
        if (DatelmisscardLists != null) {
            DatelmisscardLists.clear();
        }
        if (DatelattedanceContentArrayList != null) {
            DatelattedanceContentArrayList.clear();
        }
    }
}
